<?php
	##################################################
	#
	# Copyright (c) 2007-2008 OIC Group, Inc.
	# Written and Designed by Adam Kessler
	#
	# This file is part of Exponent
	#
	# Exponent is free software; you can redistribute
	# it and/or modify it under the terms of the GNU
	# General Public License as published by the Free
	# Software Foundation; either version 2 of the
	# License, or (at your option) any later version.
	#
	# GPL: http://www.gnu.org/licenses/gpl.txt
	#
	##################################################

	/**
	* Change logs
	* Feb 1, 2011 - trungdt - Add action map, implement SEO better
	* Feb 2, 2011 - trungdt - Add support preload actions map, then we can use it later
	* Feb 11, 2011 - trungdt - Add params map. 
	* Sep 1, 2011 - trungdt - Make SEO more compatible, more better by using style /page1/page2/page3. Now, we look the page ID by page3 instead of page1 (for using just /page1 as URL)
	*                         change in splitURL
	*                                   core_make_link  
	* Sep 26, 2011 - trungdt - Change in getSection function: we change the way we get current section when in action call: we look at the src and search in sectionref
	*/
	class router {
		private $maps = array();
		public	$url_parts = '';
		public	$current_url = '';
		public	$url_type = '';
		public	$url_style = '';
		public	$params = array();
		public	$sefPath = null;

		// feb 2, 2011 - trungdt - actions map preload
		public  $actions_map=array();

		function __construct() {
			include_once(BASE.'framework/router_maps.php');
			$this->maps = $maps;
			// feb 2, 2011 - trungdt - add actions map preload - begin
			global $db;
			$temp=$db->selectObjects("action_maps");
			$this->actions_map=array();
			foreach ($temp as $key=>$value)
			{
				$this->actions_map[$value->name]=$value;
				// Feb 11, 2011 - trungdt - Params map - begin
				$params_map=trim($value->params_map);
				$params_map=explode(" ",$params_map);
				$this->actions_map[$value->name]->params_map=$params_map;
				// Feb 11, 2011 - trungdt - Params map - end
			}
			// feb 2, 2011 - trungdt - add actions map preload - end
		}

		public function makeLink($params, $force_old_school=false) {
			$linkbase = (ENABLE_SSL ? NONSSL_URL : "");//
			$linkbase .= SCRIPT_RELATIVE;

			// 8/14/2009: Immanuel192 add this line to full support popup
			if ((SCRIPT_FILENAME!="index.php") || isset($params['router']))
			{
				$force_old_school=true;
				if (isset($params['router']))
					$script_name=$params['router'];
				else
					$script_name=SCRIPT_FILENAME;  
			}
			else
				$script_name=SCRIPT_FILENAME;

			// Check to see if SEF_URLS have been turned on in the site config
			if (SEF_URLS == 1 && ($_SERVER["PHP_SELF"] == PATH_RELATIVE.$script_name) && $force_old_school == false) {
                
                // add language into URL
                if (defined("LANG_URL_PREFIX"))
                    $linkbase.=LANG_URL_PREFIX."/";
                
				if (isset($params['section']) && !isset($params['action'])) {
					/**
					* Sep 1, 2011 - trungdt - change the way we build section type - begin
					OLD CODE
					if (isset($params['section']) && !isset($params['action'])) {
					if (empty($params['sef_name'])) {
					global $db;
					$params['sef_name'] = $db->selectValue('section', 'sef_name', 'id='.intval($params['section']));
					}
					return $linkbase.$params['sef_name'];
					} 
					*/
					global $db;
					$params['sef_name'] = $db->selectValue('section', 'sef_name', 'id='.intval($params['section']));

					$temp_nav=array();
					$temp=$db->selectObject('section','id='.intval($params['section']));
					while ($temp->parent > 0)
					{
						$temp_nav[]=$temp->sef_name;
						$temp=$db->selectObject('section','id='.$temp->parent);

					}
					$temp_nav[]=$temp->sef_name;

					// build URL
					$temp=$linkbase;
					if (substr($temp,-1) == "/")
						$temp=substr($temp,0,-1);
					while (count($temp_nav) > 0)
					{
						$temp.="/".array_pop($temp_nav);
					}
					return $temp;
					/**
					* Sep 1, 2011 - trungdt - change the way we build section type - end
					*/ 
				} else {
					// initialize the link
					$link = '';

					// we need to add the change the module parameter to controller if it exists
					// we can remove this snippit once the old modules are gone.
					if (!empty($params['module']) || empty($params['controller'])) $params['controller'] = $params['module'];
					// feb 2, 2011 - trungdt - add auto action map reverse - begin
					foreach ($this->actions_map as $key=>$value){
						if (($value->module == $params['module']) && 
						($value->action == $params['action'])
						&& (($value->views=="") || ($value->views==$params['views']) || (!isset($params['views'])) || ($params['views'] == ""))
						&& ((!isset($params['src'])) || ($params['src'] == "") || ($value->src=="") || ($value->src==$params['src']))
						)
						{
							// found this key for action map
							// unset all important key after map
							unset($params['module']);
							unset($params['action']);
							unset($params['controller']);
							unset($params['int']);

							if (isset($value->views))
							{
								unset($params['views']);
							}
							if (isset($value->src))
							{
								unset($params['src']);
							}
							// then, we build URL link
							if (isset($params['sef_name']) && ($params['sef_name'] == ""))
							{
								unset($params['sef_name']);
							}

							$link=$value->name."/";
							foreach ($params as $k=>$v)
							{
								if (($k != 'src') && ($k!='controller') && ($v!=''))
								{
									if ( in_array($k, $value->params_map))
										$link .= urlencode($params[$k])."/";
									else
										$link .= $k."/".urlencode($params[$k])."/";
								}
							}
							break;
						}
					}
					// feb 2, 2011 - trungdt - add auto action map reverse - end
					// check to see if we have a router mapping for this controller/action
					if ($link !="")
						for($i=0; $i < count($this->maps); $i++) {
							$missing_params = array("dump");
							// Feb 16, 2011 - trungdt - Change isset mode - begin
							// add isset($params['controller']) &&  in if
							if(isset($params['controller']) && in_array($params['controller'], $this->maps[$i]) && in_array($params['action'], $this->maps[$i])) {
								// Feb 16, 2011 - trungdt - Change isset mode - end
								$missing_params = array_diff_key($this->maps[$i]['url_parts'], $params);
							}

							if (count($missing_params) == 0) {
								foreach($this->maps[$i]['url_parts'] as $key=>$value) {
									if ($key == 'controller') {
										$link .= urlencode($value)."/";
									} else {
										$link .= urlencode($params[$key])."/";
									}
								}
								break;  // if this hits then we've found a match
							}
					}

					// if we found a mapping for this link then we can return it now.
					//if ($link != '') return expRouter::encode($linkbase.$link);
					if ($link != '') return $linkbase.$link;

					$link .= $params['controller'].'/';
					$link .= $params['action'].'/';
					foreach ($params as $key=>$value) {
						$value = chop($value);
						$key = chop($key);
						if ($value != "") {
							if ($key != 'module' && $key != 'action' && $key != 'controller') {
								if ($key != 'src') {
									$link .= urlencode($key)."/".urlencode($value)."/";
								} else {
									$link .= $key."/".$value."/";
								}
							}
						}
					}
					return $linkbase.$link;
				}
			} else {
				// if the users don't have SEF URL's turned on then we make the link the old school way.
				if (!empty($params['sef_name'])) unset($params['sef_name']);
				$link = $linkbase . $script_name . "?";
				foreach ($params as $key=>$value) {
					if ($key == 'router') // immanuel192 add this line to get makelink work with router params
						continue;
					$value = chop($value);
					$key = chop($key);
					if ($value != "") {
						if ($key != 'src') {
							$link .= urlencode($key)."=".urlencode($value)."&";
						} else {
							$link .= $key."=".$value."&";
						}

					}
				}

				$link = substr($link,0,-1);
				return $link; // phillip: removed htmlspecialchars so that links return without parsing & to &amp; in URL strings
				//return htmlspecialchars($link,ENT_QUOTES);
			}
		}

		public function routeRequest() {
			// Set the current url string.	This is used for flow and possible other things too
			// eDebug($_SERVER);
			//

			// start splitting the URL into it's different parts
            $this->splitURL();
			if ($this->url_style == 'sef') {
				if ($this->url_type == 'page' || $this->url_type == 'base') {
					$this->routePageRequest();				// if we hit this the formating of the URL looks like the user is trying to go to a page.
				} elseif ($this->url_type == 'action') {
					$ret = $this->routeActionRequest();  		// we didn't have a map for this URL.  Try to route it with this function.

					// if this url wasn't a valid section, or action then kill it.  It might not actually be a "bad" url, 
					// but this is a precautionary measure against bad paths on images, css & js file, etc...with the new
					// mod_rewrite rules these bad paths will not route thru here so we need to take them into account and
					// deal with them accordingly.
					if (!$ret) $this->url_type == 'malformed';  
				} elseif ($this->url_type == 'post') {
					// some forms aren't getting the controller field set right when the form is created
					// we are putting this check here to safe gaurd against a controller being referred to as 
					// a module in the form.
					if (!empty($_POST['controller']) || !empty($_POST['module'])) {
						$module = !empty($_POST['controller']) ? $_POST['controller'] : $_POST['module'];
						// Figure out if this is module or controller request - WE ONLY NEED THIS CODE UNTIL WE PULL OUT THE OLD MODULES
						if (is_readable(BASE.'framework/controllers/'.$module.'Controller.php')) {
							$_POST['controller'] = $module;
							$_REQUEST['controller'] = $module;
						}
					}
				}
			} elseif ($this->url_style == 'query' && SEF_URLS == 1 && !empty($_REQUEST['section'])) {
				// if we hit this it's an old school url coming in and we're trying to use SEF's. 
				// we will send a permanent redirect so the search engines don't freak out about 2 links pointing
				// to the same page.
				header("Location: ".$this->makeLink(array('section'=>intval($_REQUEST['section']))),TRUE,301);

			}

			// if this is a valid URL then we build out the current_url var which is used by flow, and possibly other places too
			if ($this->url_type != 'malformed') {		
				$this->current_url = $this->buildCurrentUrl();
			}

			// this will track the browse history - helps keep track of flow and will be the start of the user behavior tracking
			// Moved to the index so we have access to $section. - RAM
			//$this->updateHistory();
		}

		private function updateHistory() {
			// if we don't have everything we need then just return now
			if (empty($this->url_type)) $this->url_type = "unknown/malformed url";
			if (empty($this->current_url)) $this->current_url = "";
			// get this user's browsing history from the session
			$history = exponent_sessions_get('history');
			// make sure it's been initialize...if not do so now.
			if (empty($history[$this->url_type])) $history[$this->url_type] = array();
			// only keep the last 10 of each type of page.
			if (count($history[$this->url_type]) >= 10) array_shift($history[$this->url_type]);
			// update the history with the current URL
			$history[$this->url_type][] = $this->current_url;
			//eDebug($history);
			// put the updated history back into the session.
			exponent_sessions_set('history', $history);
		}

		public function splitURL() {
			$this->url_parts = array();
			$this->buildSEFPath();
			if (!empty($this->sefPath)) {
				$this->url_style = 'sef';
                
                if (exponent_users_isLoggedIn() && !exponent_users_isAdmin() && $_SERVER['REQUEST_METHOD'] == 'GET' && !exponent_permissions_check(array("administrate","upload","delete","meeting","meeting_delete","meeting_assistant"),exponent_core_makeLocation("profilemodule","_syscore","")) && !(strpos($this->sefPath,"logout") > 0 || strpos($this->sefPath,"mobile_") > 0))
                {
                    //  redirect the client, all request should be to client profile
                    $this->sefPath = "/profilemodule/index";
                }
                
				$this->url_parts = explode('/', $this->sefPath);
				if (empty($this->url_parts[count($this->url_parts)-1])) array_pop($this->url_parts);
				if (empty($this->url_parts[0])) array_shift($this->url_parts);
                
                // Aug 30, 2012 - Add language to URL
                // get list all of languages
                $langs = exponent_lang_full();
                if (count($this->url_parts)>0){
                    foreach ($langs as $k=>$v)
                        if (strtolower($v['url_prefix']) == strtolower($this->url_parts[0]))
                        {
                            // ok, we have language selector in our URL
                            if (!defined("LANG"))
                                define("LANG", $k);
                            
                            // check language is the same with current session language or not
                            if (defined("USE_LANG") && (LANG != USE_LANG))
                            {
                                // set this language into session, and reload this page to get the right config file
                                exponent_sessions_set('language', $v['lang_code']);  
                                header("Location: {$_SERVER['REQUEST_URI']}");
                                die();
                            }
                            
                            // set select this language as current default language
                            //exponent_sessions_set('language', $v["lang_code"]);
                            
                            // remove language out of URL stack
                            array_shift($this->url_parts);
                            
                            break;
                        }
                }
                // End Aug 30, 2012
                
				if (count($this->url_parts) < 1 || (empty($this->url_parts[0]) && count($this->url_parts) == 1) ) {
					$this->url_type = 'base';
					// Feb 1, 2011 - trungdt - implement SEO better by add action map - begin
					// remove 2 line belows because 2 line here set url_type to page 
					// but we want EXP lookup to get action map
					//} elseif (count($this->url_parts) == 1) {
					//$this->url_type = 'page';
					// Feb 1, 2011 - trungdt - implement SEO better by add action map - end    
					// Sep 26. 2011 - trungdt - Fix bug: when you post request in URL style, Exponent does not regconize because $_POST empty
				} elseif (($_SERVER['REQUEST_METHOD'] == 'POST') && (count($_POST) > 0)) {
					$this->url_type = 'post';
				} else {
					// take a peek and see if a page exists with the same name as the first value...if so we probably have a page with
					// extra perms...like printerfriendly=1 or ajax=1;
					// feb 1, 2011 - trungdt - add action map - begin
					// first we lookup action map
					global $db;
					$temp=null;
					if (isset($this->actions_map[$this->url_parts[0]])){
						$temp=$this->actions_map[$this->url_parts[0]];
					}
					if ($temp) // we must lookup to get action map
					{
						// remove first element
						array_shift($this->url_parts);
						// and we fake data like normal
						$actions_map=array();
						$actions_map[]=$temp->module;
						$actions_map[]=$temp->action;

						if ($temp->views!="")
						{
							$actions_map[]="view";
							$actions_map[]=$temp->views;    
						}
						if ($temp->src!="")
						{
							//$this->url_parts[]=$temp->src;
							$actions_map[]="src";
							$actions_map[]=$temp->src;
						}
						// Feb 11 - 2011 - trungdt - Add params map - begin
						foreach ($temp->params_map as $k=>$v)
						{
							if ((count($this->url_parts)>0) && ($v!= ""))
							{
								// insert a pair
								$actions_map[]=$v;
								$actions_map[]=$this->url_parts[0];
								array_shift($this->url_parts);
							}
							else
							{
								break;
							}
						}
						// Feb 11 - 2011 - trungdt - Add params map - end
						$this->url_parts=array_merge($actions_map,$this->url_parts);  
						$this->url_type = 'action';
					}
					else 
						// feb 1, 2011 - trungdt - add action map - end 
						// Feb 10, 2011 - trungdt - remove this line in if condition to fix but dont detect sef page 
						//(in_array('printerfriendly', $this->url_parts)
						if ( ($db->selectObject('section', "sef_name='".$this->url_parts[0]."'") != null) ) {
							$this->url_type = 'page';
						} else {
							/**
							* Sep 30, 2011 - trungdt - Fix bug: when in action call, we MUST detect the module and action params
							* Other else, transfer to the search action
							*/
							if (count($this->url_parts)==1 || ((count($this->url_parts) >=2) && ($this->url_parts[1] !='index') && !in_array("_common", $this->url_parts) && 
								!is_readable(BASE."modules/".$this->url_parts[0]."/actions/".$this->url_parts[1].".php")))
							{
								/*$temp=implode("/",$this->url_parts);
								$this->url_parts[0]="searchmodule";
								$this->url_parts[1]="search";
								$this->url_parts[2]="search_string";
								$this->url_parts[3]=$temp;
							}
							    $this->url_type = 'action';	*/
                                $this->url_parts=array();
                            }
                            $this->url_type = 'action';
					}
				}

				$this->params = $this->convertPartsToParams();
			} elseif (isset($_SERVER['REQUEST_URI'])) {
				// if we hit here, we don't really need to do much.	 All the pertinent info will come thru in the POST/GET vars
				// so we don't really need to worry about what the URL looks like.
				$this->url_style = 'query'; 
			} 
		}

		public function routePageRequest() {		
			global $db;

			if ($this->url_type == 'base') {
				// if we made it in here this is a request for http://www.baseurl.com
				$_REQUEST['section'] = SITE_DEFAULT_SECTION;  
			} else {
				// Try to look up the page by sef_name first.  If that doesn't exist, strip out the dashes and 
				// check the regular page names.  If that still doesn't work then we'll redirect them to the 
				// search module using the page name as the seach string.
				/**
				* Sep 1, 2011 - trungdt - Change this code to more compatible with SEO - begin
				*  Instead of get $section by the first element of $this->url_parts, we get $section from the last element, because, the last element is the real section. Now, the first element only the flag for Exponent to know that this is a section browing style
				*/
				
				/**
				* Sep 1, 2011 - trungdt - Change this code to more compatible with SEO - end
				* 
				*/
				
				// OLD CODE - $section = $this->getPageByName($this->url_parts[0]);
				$section = $this->getPageByName($this->url_parts[count($this->url_parts)-1]);

				$_REQUEST['section'] = $section->id;
			}
			return true;
		}

		public function routeActionRequest() {
			$return_params = array('controller'=>'','action'=>'','url_parts'=>array());

			// If we have three url parts we assume they are controller->action->id, otherwise split them out into name<=>value pairs
			$return_params['controller'] = $this->url_parts[0];	// set the controller/module
			// feb 1, 2011 - trungdt - add this check to fix null action check
			if (isset($this->url_parts[1]))
				$return_params['action'] = $this->url_parts[1];		// set the action

			// Figure out if this is module or controller request - WE ONLY NEED THIS CODE UNTIL WE PULL OUT THE OLD MODULES
			if (is_readable(BASE.'controllers/'.$return_params['controller'].'Controller.php')) {
				$requestType = 'controller';
			} elseif (is_dir(BASE.'modules/'.$return_params['controller'])) {
				$requestType = 'module';
			} else {
				return false;  //this is an invalid url return an let the calling function deal with it.
			}

			// now figure out the name<=>value pairs
			if (count($this->url_parts) == 3) {
				if ( is_numeric($this->url_parts[2])) {
					$return_params['url_parts']['id'] = $this->url_parts[2];
				}
			} else {
				for($i=2; $i < count($this->url_parts); $i++ ) {
					if ($i % 2 == 0) {
						$return_params['url_parts'][$this->url_parts[$i]] = isset($this->url_parts[$i+1]) ? $this->url_parts[$i+1] : '';
					}
				}
			}		 

			// Set the module or controller - this how the actual routing happens
			$_REQUEST[$requestType] = $return_params['controller']; //url_parts[0];
			$_GET[$requestType] = $return_params['controller'];
			$_POST[$requestType] = $return_params['controller'];

			// Set the action for this module or controller
			if ($_SERVER['REQUEST_METHOD'] == 'POST') {
				// most of the time we can just grab the action outta the POST array since this is passed as a hidden field, 
				// but sometimes it is actually set as the action on the form itself...then we get it from the params array instead.
				$action = !empty($_POST['action']) ? $_POST['action'] : $this->params['action'];
			} else {
				$action = $return_params['action'];
			}

			$_REQUEST['action'] = $action;
			$_GET['action'] = $action;
			$_POST['action'] = $action;

			// pass off the name<=>value pairs
			foreach($return_params['url_parts'] as $key=>$value) {
				//$save_value = is_numeric($value) ? $value: router::decode($value);
				$save_value = $value;
				$_REQUEST[$key] = $save_value;
				$_GET[$key] = $save_value;
			}

			return true;
		}

		public function buildCurrentUrl() {
			$url =	URL_BASE;
			if ($this->url_style == 'sef') {
				$url .= substr(PATH_RELATIVE,0,-1).$this->sefPath;
			} else {
				$url .= (empty($_SERVER['REQUEST_URI'])) ? $_ENV['REQUEST_URI'] : $_SERVER['REQUEST_URI'];
			}
			return $url;
		}

		public static function encode($url) {
			$url = str_replace('&', 'and', $url);
			return preg_replace("/(-)$/", "", preg_replace('/(-){2,}/', '-', strtolower(preg_replace("/([^0-9a-z-_\+])/i", '-', $url))));	
		}

		public static function decode($url) {
			$url = str_replace('-', ' ', $url);
			return str_replace('+', '-', $url);
		}

		public function getSefUrlByPageId($id=null) {
			if (!empty($id)) {
				global $db;
				$section = $db->selectObject('section', 'id='.intval($id));
				$url = URL_FULL;
				$url .= !empty($section->sef_name) ? $section->sef_name : $section->name;
			}
		}

		public function buildUrlByPageId($id=null) {
			if (!empty($id)) {
				global $db;
				//$url = URL_FULL;
				$url = '';
				if (SEF_URLS == 1) {
					$section = $db->selectObject('section', 'id='.intval($id));
					$url .= !empty($section->sef_name) ? $section->sef_name : $section->name;
				} else {
					$url .= 'index.php?section='.$id;
				}
			}

			return $url;
		}

		public function printerFriendlyLink($link_text="Printer Friendly", $class=null, $width=800, $height=600) {
			$url = '';
			if (PRINTER_FRIENDLY != 1) {
				$class = !empty($class) ? $class : 'printer-friendly-link';
				$url =	'<a class="'.$class.'" href="javascript:void(0)" onclick="window.open(\'';
				if ($this->url_style == 'sef') {
					$url .= $this->convertToOldSchoolUrl();
					if ($this->url_type=='base') $url .= 'index.php?section='.SITE_DEFAULT_SECTION;
				} else {
					$url .= $this->current_url;
				}

				$url .= '&printerfriendly=1\' , \'mywindow\',\'menubar=1,resizable=1,width='.$width.',height='.$height.'\');"';
				$url .= '>'.$link_text.'</a>';
			}

			return $url; 
		}

		public function convertToOldSchoolUrl() {
			return $this->makeLink($this->params, true);
		}

		public function convertPartsToParams() {
			$params=array();
			if ($this->url_type == 'base') {
				$params['section'] = SITE_DEFAULT_SECTION;
			} elseif ($this->url_type == 'page') {
				/**
				* Sep 1, 2011 - trungdt - Change this code to more compatible with SEO - begin
				*  Instead of get $section by the first element of $this->url_parts, we get $section from the last element, because, the last element is the real section. Now, the first element only the flag for Exponent to know that this is a section browing style
				*/
				$section = $this->getPageByName($this->url_parts[count($this->url_parts)-1]);
				/**
				* Sep 1, 2011 - trungdt - Change this code to more compatible with SEO - end
				* 
				*/
				$params['section'] = $section->id;
			} elseif ($this->url_type == 'action') {
				$params['module'] = $this->url_parts[0];
				// feb 1, 2011 - trungdt - add this check to fix null action check
				if (isset($this->url_parts[1]))
					$params['action'] = $this->url_parts[1];
				for($i=2; $i < count($this->url_parts); $i++ ) {
					if ($i % 2 == 0) {
						$params[$this->url_parts[$i]] = isset($this->url_parts[$i+1]) ? $this->url_parts[$i+1] : '';
					}
				}
			}

			return $params;
		}

		public function getPageByName($url_name) {
			global $db;
			if ($this->url_type == 'base') {
				// if we made it in here this is a request for http://www.baseurl.com
				$section = $db->selectObject('section', 'id='.SITE_DEFAULT_SECTION);
			} else {
				$section = $db->selectObject('section', "sef_name='".$url_name."'");
				/*if (empty($section)) {
				$url_name = router::decode($url_name);
				//$name = str_replace('-', ' ', $url_name);
				$name2 = str_replace(' ', '&nbsp;', $url_name);
				$section = $db->selectObject('section', 'name="'.$url_name.'" OR name="'.$name2.'"');
				}*/
			}
			// if section is still empty then we should route the user to the search (cool new feature :-) )
			// at some point we need to write a special action/view for the search module that lets the user
			// know they were redirected to search since the page they tried to go to directly didn't exist.
			if (empty($section)) {
				header("Refresh: 0; url=".$this->makeLink(array('module'=>'searchmodule', 'action'=>'search', 'search_string'=>$this->url_parts[0])), false, 404);
				exit();
			} else {
				return $section;
			}
		}

		private function buildSEFPath () {
			// Apache
			if (strpos($_SERVER['SERVER_SOFTWARE'],'Apache') === 0) {
				switch(php_sapi_name()) {
					case "cgi":
						$this->sefPath = !empty($_ENV['REQUEST_URI']) ? urldecode($_ENV['REQUEST_URI']): null;
						break;
					case "cgi-fcgi":
						@$this->sefPath = ($_SERVER['REDIRECT_URL'] != PATH_RELATIVE.'index.php') ? urldecode($_SERVER['REDIRECT_URL']) : urldecode($_ENV['REQUEST_URI']);
						break;
					default:
						$this->sefPath = !empty($_SERVER['REDIRECT_URL']) ? urldecode($_SERVER['REDIRECT_URL']) : null;
						break;
				}
				// Lighty
			} elseif (strpos($_SERVER['SERVER_SOFTWARE'],'lighttpd') === 0) {
				if (isset($_SERVER['ORIG_PATH_INFO'])) {
					$this->sefPath = urldecode($_SERVER['ORIG_PATH_INFO']);
				} elseif (isset($_SERVER['REDIRECT_URI'])){
					$this->sefPath = urldecode(substr($_SERVER['REDIRECT_URI'],9));
				}
			}
			else
			{
				// Jul 29, 2011 - trungdt - Add this for LiteSpeed web server compatible
				$this->sefPath = !empty($_SERVER['REQUEST_URI']) ? urldecode($_SERVER['REQUEST_URI']): null;
			}
			@$this->sefPath = substr($this->sefPath,strlen(substr(PATH_RELATIVE,0,-1)));	
			if (strpos($this->sefPath,'/index.php') === 0) {
				$this->sefPath = null;
			}
		}

		public function getSection() {
			// Check if this was a printer friendly link request
			if (!defined("PRINTER_FRIENDLY"))
				define('PRINTER_FRIENDLY', isset($_REQUEST['printerfriendly']) ? 1 : 0);

			if (isset($_REQUEST['action']) && isset($_REQUEST['module'])) {
				if (isset($_REQUEST['section'])) {
					$section = $this->url_type=="sef" ? $this->getPageByName($_REQUEST['section']) : $_REQUEST['section'] ;
				} else {
					/**
					* Sep 26, 2011 -trungdt - Change the way we get section id. Now we should looking at the src param to get the right section id
					*/
					if (isset($_REQUEST['src']))
					{
						global $db;
						$temp=$db->selectObject("sectionref", "source = '{$_REQUEST['src']}'");
						if ($temp)
							$section=$temp->section;
						else
							$section = (exponent_sessions_isset('last_section') ? exponent_sessions_get('last_section') : SITE_DEFAULT_SECTION);
					}
					else
						$section = (exponent_sessions_isset('last_section') ? exponent_sessions_get('last_section') : SITE_DEFAULT_SECTION);
					/**
					* Sep 26, 2011 - trungdt - End change
					*/
				}
			} else {
				$section = (isset($_REQUEST['section']) ? $_REQUEST['section'] : SITE_DEFAULT_SECTION);
			}
			return $section;
		}

		public function getSectionObj($section) {
			global $db;
			$sectionObj = $db->selectObject('section','id='. intval($section));
			if (!defined("AUTHORIZED_SECTION"))
			{
				if (!navigationmodule::canView($sectionObj)) {
					define('AUTHORIZED_SECTION',0);
				} else {
					define('AUTHORIZED_SECTION',1);
				}
			}
			if (!defined("PUBLIC_SECTION"))
			{
				if (!navigationmodule::isPublic($sectionObj)) {
					define('PUBLIC_SECTION',0);
				} else {
					define('PUBLIC_SECTION',1);
				}
			}

			if (isset($_REQUEST['section'])) {
				exponent_sessions_set('last_section', intval($_REQUEST['section']));
			} else {
				//exponent_sessions_unset('last_section');
			}
			return $sectionObj;
		}
	}
?>
